require 'utils'
require 'cutils'
require 'dict'

function create_sampler_storage() 

	-- load data
	print('load data')
	local f = torch.DiskFile(ctx_data_path, 'r')
	f:binary()
	local data = f:readObject():double()
	f:close()

	-- load dic
	print('load dic')
	local f = torch.DiskFile(dic_emb_path, 'r')
	dic = f:readObject()
	setmetatable(dic, Dict_mt)
	f:close()
	
	-- create ws (alias, prob) and w_prob
	print('create sampler storage')
	w_prob = torch.histc(data[{1,{}}], dic:size(), 1, dic:size())
	w_prob:div(w_prob:sum())
	w_ws = {}
	w_ws.alias, w_ws.prob = init_alias_sampling(w_prob)

	cws = data[{{2,-1},{}}]
	cw_prob = torch.histc(cws:resize(cws:numel()), dic:size(), 1, dic:size())
	cw_prob = cw_prob:gt(0):double()
	cw_prob:div(cw_prob:sum())
	cw_ws = {}
	cw_ws.alias, cw_ws.prob = init_alias_sampling(cw_prob)

	sampler_storage = {
			cw_ws = cw_ws, cw_prob = cw_prob,
			w_ws = {w_ws}, w_prob = {w_prob} 
		}

	-- save to file
	print('save to file')
	f = torch.DiskFile(output_path, 'w')
	f:binary()
	f:writeObject(sampler_storage)
	f:close()
end

if #arg ~= 3 then 
	print(
		"<ctx_data_path [torch format]> " ..
		"<dic_emb_path>  " ..
		"<output_path>")
else
	ctx_data_path	= arg[1]
	dic_emb_path	= arg[2]
	output_path		= arg[3]
	create_sampler_storage()
end


